package com.lb.jwitter.action;

import java.util.ArrayList;
import java.util.List;
import com.lb.jwitter.util.ApplicationContextProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.util.Message;
import java.util.Map;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.Action;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.service.TblCTDTThayTheMonHoc;
import com.lb.jwitter.service.TblCTDTThayTheMonHocService;

/**
 * @author QUOCVIET
 * 
 */

@ContextConfiguration(locations = "classpath:beans.xml")
public class TblCTDTThayTheMonHocSearchAction {
	private int pageSize;

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	private int page;

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage() {
		return page;
	}

	private int pages;

	public void setPages(int pages) {
		this.pages = pages;
	}

	public int getPages() {
		return pages;
	}

	private String dataString;

	public void setDataString(String dataString) {
		this.dataString = dataString;
	}

	public String getDataString() {
		return dataString;
	}

	private Message msg;

	public Message getMsg() {
		return msg;
	}

	private Long monHoc1Id;

	public void setMonHoc1Id(Long monHoc1Id) {
		this.monHoc1Id = monHoc1Id;
	}

	public Long getMonHoc1Id() {
		return monHoc1Id;
	}

	// ----------------chumano------------------
	private String maMonHoc1;

	public void setMaMonHoc1(String monhoc) {
		this.maMonHoc1 = monhoc;
	}

	public String getMaMonHoc1() {
		return maMonHoc1;
	}
	
	private String maMonHoc2;

	public void setMaMonHoc2(String monhoc) {
		this.maMonHoc2 = monhoc;
	}

	public String getMaMonHoc2() {
		return maMonHoc2;
	}
	
	private String maMHThayThe1;

	public void setMaMHThayThe1(String monhoc) {
		this.maMHThayThe1 = monhoc;
	}

	public String getMaMHThayThe1() {
		return maMHThayThe1;
	}
	
	private String maMHThayThe2;

	public void setMaMHThayThe2(String monhoc) {
		this.maMHThayThe2 = monhoc;
	}

	public String getMaMHThayThe2() {
		return maMHThayThe2;
	}
	
	private String maKhoiNganh;

	public void setMaKhoiNganh(String  maKhoiNganh) {
		this. maKhoiNganh =  maKhoiNganh;
	}

	public String getMaKhoiNganh() {
		return  maKhoiNganh;
	}
	// ==============================================

	private Long mHThayThe1Id;

	public void setMHThayThe1Id(Long mHThayThe1Id) {
		this.mHThayThe1Id = mHThayThe1Id;
	}

	public Long getMHThayThe1Id() {
		return mHThayThe1Id;
	}

	private int tukhoaHoc;

	public void setTuKhoaHoc(int tukhoaHoc) {
		this.tukhoaHoc = tukhoaHoc;
	}

	public int getTuKhoaHoc() {
		return tukhoaHoc;
	}

	private int denkhoaHoc;

	public void setDenKhoaHoc(int denkhoaHoc) {
		this.denkhoaHoc = denkhoaHoc;
	}

	public int getDenKhoaHoc() {
		return denkhoaHoc;
	}

	private List<TblCTDTThayTheMonHoc> tblCTDTThayTheMonHocs;

	public List<TblCTDTThayTheMonHoc> getTblCTDTThayTheMonHocs() {
		return tblCTDTThayTheMonHocs;
	}

	private TblCTDTThayTheMonHocService tblCTDTThayTheMonHocService;

	private int preSearch(Message msg, List<ConditionReport> cons) {
		msg.setPreMessage_b(true);
		msg.setPreMessage_s("success");
		return 1;
	}

	private int postSearch(Message msg, List<ConditionReport> cons) {
		msg.setPostMessage_b(true);
		msg.setPostMessage_s("success");
		return 1;
	}

	private TblTaiKhoan taiKhoanDangNhap;

	public TblTaiKhoan getTaiKhoanDangNhap() {
		return taiKhoanDangNhap;
	}

	public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) {
		this.taiKhoanDangNhap = taiKhoanDangNhap;
	}

	private TblQuyenChiTietService tblQuyenChiTietService;
	@Autowired
	// Injected by Spring
	ApplicationContextProvider contextProvider;

	public String execute() throws Exception {
		try {
			Map session = ActionContext.getContext().getSession();
			taiKhoanDangNhap = (TblTaiKhoan) session.get("taiKhoan");
			if (taiKhoanDangNhap == null)
				return "noprivilege";
			ApplicationContext context = ApplicationContextProvider
					.getApplicationContext();
			tblQuyenChiTietService = (TblQuyenChiTietService) context
					.getBean("tbl_Quyen_ChiTietClient");
			if (!tblQuyenChiTietService.checkRight(taiKhoanDangNhap
					.getMaNhomQuyen().getId(), "tbl_CTDT_ThayTheMonHoc", "xem")) {
				return "noprivilege";
			}
			tblCTDTThayTheMonHocService = (TblCTDTThayTheMonHocService) context
					.getBean("tbl_CTDT_ThayTheMonHocClient");
			msg = new Message();
			List<ConditionReport> cons = new ArrayList<ConditionReport>();
			if (monHoc1Id!=null &&monHoc1Id != 0) {
				ConditionReport con = new ConditionReport();
				con.setCol("monHoc1Id.id");
				con.setValue1(new Long(monHoc1Id));
				con.setOperator("eq");
				cons.add(con);
			}
			if (mHThayThe1Id!=null &&mHThayThe1Id != 0) {
				ConditionReport con = new ConditionReport();
				con.setCol("MHThayThe1Id.id");
				con.setValue1(new Long(mHThayThe1Id));
				con.setOperator("eq");
				cons.add(con);
			}
			
			//----------------chumano---------------
			if (maMonHoc1 != null && !maMonHoc1.equals("")) {
				ConditionReport con = new ConditionReport();
				con.setCol("monHoc1Id.ma");
				con.setValue1(maMonHoc1);
				con.setOperator("eq");
				cons.add(con);
			}
			
			if (maMonHoc2 != null && !maMonHoc2.equals("")) {
				ConditionReport con = new ConditionReport();
				con.setCol("monHoc2Id.ma");
				con.setValue1(maMonHoc2);
				con.setOperator("eq");
				cons.add(con);
			}
			
			if (maMHThayThe1 != null && !maMHThayThe1.equals("")) {
				ConditionReport con = new ConditionReport();
				con.setCol("MHThayThe1Id.ma");
				con.setValue1(maMHThayThe1);
				con.setOperator("eq");
				cons.add(con);
			}
			
			if (maMHThayThe2 != null && !maMHThayThe2.equals("")) {
				ConditionReport con = new ConditionReport();
				con.setCol("MHThayThe2Id.ma");
				con.setValue1(maMHThayThe2);
				con.setOperator("eq");
				cons.add(con);
			}
			
			if (maKhoiNganh != null && !maKhoiNganh.equals("")) {
				ConditionReport con = new ConditionReport();
				con.setCol("khoiNganhId.ma");
				con.setValue1(maKhoiNganh);
				con.setOperator("eq");
				cons.add(con);
			}
			//======================================
			if (tukhoaHoc != 0) {
				ConditionReport con = new ConditionReport();
				con.setCol("khoaHoc");
				con.setValue1(tukhoaHoc);
				if (denkhoaHoc != 0) {
					con.setValue2(denkhoaHoc);
					con.setOperator("between");
				} else {
					con.setOperator("ge");
				}
				cons.add(con);
			} else if (denkhoaHoc != 0) {
				ConditionReport con = new ConditionReport();
				con.setCol("khoaHoc");
				con.setValue1(denkhoaHoc);
				con.setOperator("le");
				cons.add(con);
			}
			preSearch(msg, cons);
			if (msg.isPreMessage_b()) {
				if (pageSize == 0) {
					pageSize = 20;
				}
				if (page == 0) {
					page = 1;
				}
				tblCTDTThayTheMonHocs = tblCTDTThayTheMonHocService
						.findtblCTDTThayTheMonHocByCondition(cons, pageSize,
								page);
				pages = tblCTDTThayTheMonHocService
						.counttblCTDTThayTheMonHocByCondition(cons)
						/ pageSize + 1;
				postSearch(msg, cons);
				if (msg.isPostMessage_b() == false) {
					return Action.ERROR;
				}
			} else {
				return Action.ERROR;
			}
			dataString = "";
			dataString += "&maMonHoc1=" + maMonHoc1;
			dataString += "&maMonHoc2=" + maMonHoc2;
			dataString += "&maMHThayThe1=" + maMHThayThe1;
			dataString += "&tukhoaHoc=" + tukhoaHoc;
			dataString += "&denkhoaHoc=" + denkhoaHoc;
			return Action.SUCCESS;
		} catch (Exception e) {
			System.out.println("Loi Seach tbl_CTDT_ThayTheMonHoc Action");
			e.printStackTrace();
			return Action.ERROR;
		}
	}
}
